<template>
	<view>
		<view class="search-box">
			<uni-search-bar  @input="input" :radius="100" cancelButton="none" focus="true">
			</uni-search-bar>
		</view>
		<!-- 搜索建议列表   v-if切换搜索建议列表和搜索历史 -->
		<view class="sugg-list" v-if="searchResults.length !== 0">
			<view class="sugg-item" v-for="(item,i) in searchResults" :key="i" @click="gotoDetail(item)">
				
				<view class="goods-name"><uni-icons type="search" size="20" class="icon-search"></uni-icons>{{item.goods_name}}</view>
				<uni-icons type="arrowright" size="20"></uni-icons>
			</view>
		</view>
		<!-- 搜索历史 -->
		<view class="history-box" v-else>
			<!-- 标题区域 -->
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons type="trash" size="20" @click="clearHistory"></uni-icons>
			</view>
			<!-- 列表区域 -->
			<view class="history-lsit">
				<uni-tag :text="item" v-for="(item,i) in histories" :key="i" @click="gotoGoodsList(item)"></uni-tag>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				timer: null,
				//搜索的关键字
				kw: '',
				//搜索的关键列表
				searchResults: [],
				//历史搜索列表
				historyList:[]
			};
		},
		onLoad() {
			//页面加载时就获取到历史记录
			this.historyList =  JSON.parse(uni.getStorageSync('kw') || '[]')
		},
		methods: {
			//input舒适事件的处理函数
			input(e) {
				// console.log(e)
				//防抖
				clearTimeout(this.timer)
				this.timer = setTimeout(() => {
					this.kw = e
					this.getSearchList()
				}, 500)
			},
			//获取搜索列表数据
			async getSearchList() {
				if (this.kw.trim() === '') {
					this.searchResults = []
					return
				}
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/goods/qsearch', {
					query: this.kw
				})
				if (res.meta.status !== 200) return uni.$showMsg()
				this.searchResults = res.message
				//保存搜索的历史记录
				this.saveSearchHistory()
			},
			//去往商品详情页
			gotoDetail(item){
				console.log(item.goods_id)
				uni.navigateTo({ 
					url:'/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
				})
			},
			//数组去重
			saveSearchHistory(){
				
				// this.historyList.push(this.kw)
				//数组去重
				const set = new Set(this.historyList)
				//这里先删除后添加，才能把最新的搜索放在前面
				set.delete(this.kw)
				set.add(this.kw)
				//将set对象转化为数组形式
				this.historyList = Array.from(set)
				
				//对搜索历史数据进行永久性保存 uni.setStorageSync
				uni.setStorageSync('kw',JSON.stringify(this.historyList))
			},
			//清空历史数据
			clearHistory(){
				this.historyList = []
				uni.clearStorageSync('kw')
			},
			//点击历史详情跳转商品页面
			gotoGoodsList(kw){
				uni.navigateTo({
					url:'/subpkg/goods_list/goods_list?query=' + kw
				})
			}
		},
		computed:{
			histories(){
				return [...this.historyList].reverse() 
			}
		}
	}
</script>

<style lang="scss">
	.search-box {
		position: sticky;
		top: 0;
		z-index: 99;
	}

	.sugg-list {
         padding: 5px;

		.sugg-item {
			border-bottom: 1px solid #efefef;
			font-size: 14px;
			padding: 10px 0;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.goods-name {
				text-overflow: ellipsis;
				white-space: nowrap;
				overflow: hidden;
				margin: 0 3px;
				.icon-search{
					margin-right: 5px;
				}
			}
		}
	}
	.history-box{
		padding: 0 5px;
		.history-title{
			display: flex;
			justify-content: space-between;
			height: 40px;
			align-items: center;
			border-bottom: 1px solid #efefef;
		}
		.history-lsit{
			display: flex;
			flex-wrap: wrap;
			margin: 5px;
			.uni-tag{
				margin: 5px 5px 0 0 ;
			}
		}
	}
</style>
